<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form ref="formRef" @keyup.enter="baTable.onSubmit(formRef)" :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'" :rules="rules" v-if="!baTable.form.loading">
                    <el-form-item prop="level" label="VIP等级">
                        <el-input v-model="baTable.form.items!.level" type="string"
                            :placeholder="t('Please input field', { field: 'VIP等级' })"></el-input>
                    </el-form-item>
                    <el-form-item prop="name" label="名称">
                        <el-input v-model="baTable.form.items!.name" type="string"
                            :placeholder="t('Please input field', { field: '名称' })"></el-input>
                    </el-form-item>
                    <FormItem label="图片" type="image" v-model="baTable.form.items!.image" />
                    <FormItem label="内容" type="editor" v-model="baTable.form.items!.content" />
                    <el-form-item prop="sales" label="价格配置">
                        <div style="flex: 1">
                            <VueDraggable v-model="priceList" target=".el-table__body tbody" handle=".handle"
                                :animation="150">
                                <el-table :data="priceList" style="width: 100%">
                                    <el-table-column prop="title" label="标题" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.title" type="string"
                                                :placeholder="t('Please input field', { field: '标题' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="days" label="天数" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.days" type="number"
                                                :placeholder="t('Please input field', { field: '天数' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="price" label="价格" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.price" type="number"
                                                :placeholder="t('Please input field', { field: '价格' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="description" label="描述" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.description" type="string"
                                                :placeholder="t('Please input field', { field: '描述' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="unit" label="单位" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.unit" type="string"
                                                :placeholder="t('Please input field', { field: '单位' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="default" label="默认">
                                        <template #default="scope">
                                            <el-select v-model="scope.row.default" placeholder="请选择">
                                                <el-option label="是" value="1"></el-option>
                                                <el-option label="否" value="0"></el-option>
                                            </el-select>
                                        </template>
                                    </el-table-column>
                                    <el-table-column fixed="right" label="操作" min-width="120">
                                        <template #default="scope">
                                            <el-button type="primary" size="small" @click.prevent="deleteRow(scope.$index, 'price')">
                                                <el-icon>
                                                    <Delete />
                                                </el-icon>
                                            </el-button>
                                            <el-button type="primary" class="handle" size="small">
                                                <el-icon>
                                                    <Rank />
                                                </el-icon>
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </VueDraggable>
                            <div class="table-button">
                                 <el-button type="primary" class="handle" @click="addRow('price')">
                                <el-icon style="vertical-align: middle">
                                    <Plus />
                                </el-icon>
                                <span style="vertical-align: middle"> 增加 </span>
                            </el-button>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item prop="sales" label="权益配置">
                          <div style="flex: 1">
                            <VueDraggable v-model="rightList" target=".el-table__body tbody" handle=".handle"
                                :animation="150">
                                <el-table :data="rightList" style="width: 100%">
                                    <el-table-column prop="text" label="名称" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.text" type="string"
                                                :placeholder="t('Please input field', { field: '名称' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="intro" label="介绍" >
                                        <template #default="scope">
                                            <el-input v-model="scope.row.intro" type="string"
                                                :placeholder="t('Please input field', { field: '介绍' })"></el-input>
                                        </template>
                                    </el-table-column>
                                    <el-table-column prop="image" label="图标">
                                        <template #default="scope">
                                            <div>
                                             <FormItem class="custom-size-image" type="image" v-model="scope.row!.image" />
                                            </div>
                                        </template>
                                    </el-table-column>
                                    <el-table-column fixed="right" label="操作" width="120">
                                        <template #default="scope">
                                            <el-button type="primary" size="small" @click.prevent="deleteRow(scope.$index, 'right')">
                                                <el-icon>
                                                    <Delete />
                                                </el-icon>
                                            </el-button>
                                            <el-button type="primary" class="handle" size="small">
                                                <el-icon>
                                                    <Rank />
                                                </el-icon>
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </VueDraggable>
                            <div class="table-button">
                                 <el-button type="primary" class="handle" @click="addRow('right')">
                                <el-icon style="vertical-align: middle">
                                    <Plus />
                                </el-icon>
                                <span style="vertical-align: middle"> 增加 </span>
                            </el-button>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item prop="sales" label="销量">
                        <el-input v-model="baTable.form.items!.sales" type="string"
                            :placeholder="t('Please input field', { field: '销量' })"></el-input>
                    </el-form-item>
                    <FormItem :label="t('State')" v-model="baTable.form.items!.status" type="radio" :input-attr="{
                        border: true,
                        content: { normal: '正常', hidden: '隐藏', pulloff: '下架' },
                    }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                        t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, inject, useTemplateRef, ref, defineExpose } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { dayjs, type FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import { Delete, Plus, Rank } from '@element-plus/icons-vue'
import { VueDraggable } from 'vue-draggable-plus'

const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()
const priceList = ref<any[]>([])
const rightList = ref<any[]>([])

//删除行
const deleteRow = (index: number, type: string) => {
    if (type === 'price') {
        priceList.value.splice(index, 1)
    } else if (type === 'right') {
        rightList.value.splice(index, 1)
    }
}

// 增加行
const addRow = (type: string) => {
    if (type === 'price') {
        const temp = JSON.parse(JSON.stringify(priceList.value))
        priceList.value = []
        temp.push({
            title: '',
            days: '',
            price: '',
            desc: '',
            unit: '',
            default: '1',
        })
        setTimeout(() => {
            temp.forEach((item: any) => {
                priceList.value.push(item)
            })
        }, 50)
    } else if (type === 'right') {
        const temp = JSON.parse(JSON.stringify(rightList.value))
        rightList.value = []
        temp.push({
            text: '',
            intro: '',
            image: '',
        })
        setTimeout(() => {
            temp.forEach((item: any) => {
                rightList.value.push(item)
            })
        }, 50)
    }
}

const rules: Partial<Record<string, FormItemRule[]>> = reactive({})

defineExpose({
    priceList,
    rightList,
})
</script>

<style scoped lang="scss">
.custom-size-image :deep(.el-upload-list--picture-card) {
    --el-upload-list-picture-card-size: 60px;
}
.custom-size-image :deep(.el-upload--picture-card) {
    --el-upload-picture-card-size: 60px;
}

.table-button {
    display: flex;
    justify-content: end;
    margin-top: 10px;
}
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}

.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}

.avatar {
    width: 110px;
    height: 110px;
    display: block;
}

.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
